﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class supportController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static supportInfo getbyID(int supportID)
        {
            supportInfo objSupport = new supportInfo();
            using (SqlCommand cmd = new SqlCommand("getIDSupport", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@supportID", supportID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objSupport.supportID = dr.GetInt32(0);
                        objSupport.nickname = dr.GetString(1);
                        objSupport.caption = dr.GetString(2);
                        objSupport.phone = dr.GetString(3);
                    }
                }
                dr.Close();
            }
            return objSupport;
        }

        #endregion

        #region getbyAll
        public static DataTable ListSupport()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listSupport", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertSupport(supportInfo objSupport)
        {
            using (SqlCommand cmd = new SqlCommand("insertSupport", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@nickname", objSupport.nickname));
                cmd.Parameters.Add(new SqlParameter("@caption", objSupport.caption));
                cmd.Parameters.Add(new SqlParameter("@phone", objSupport.phone));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateSupport(supportInfo objSupport)
        {
            using (SqlCommand cmd = new SqlCommand("updateSupport", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@supportID", objSupport.supportID));
                cmd.Parameters.Add(new SqlParameter("@nickname", objSupport.nickname));
                cmd.Parameters.Add(new SqlParameter("@caption", objSupport.caption));
                cmd.Parameters.Add(new SqlParameter("@phone", objSupport.phone));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteSupport(int supportID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteSupport", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@supportID", supportID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
    }
}
